﻿<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="()=>{this.visible=false}"
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="计量单位组编码" prop="Code">
          <a-input v-model="entity.Code" autocomplete="off" placeholder="计量单位组编码" />
        </a-form-model-item>
        <a-form-model-item label="计量单位组名称" prop="Name">
          <a-input v-model="entity.Name" autocomplete="off" placeholder="计量单位组名称" />
        </a-form-model-item>
        <a-form-model-item label="是否默认" prop="IsDefault">
          <a-radio-group v-model="entity.IsDefault" :default-value="false" button-style="solid">
            <a-radio-button :value="false"> 否 </a-radio-button>
            <a-radio-button :value="true"> 是 </a-radio-button>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="类型选择" prop="GroupType">
          <a-radio-group v-model="entity.GroupType" :default-value="false" button-style="solid">
            <a-radio-button :value="false">固定换算</a-radio-button>
            <a-radio-button :value="true">浮动换算</a-radio-button>
          </a-radio-group>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    parentObj: {
      type: Object,
      default () {
        return {}
      }
    },
    PDeptId: { default: '', type: String } // 组织ID
  },
  data () {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {},
      title: ''
    }
  },
  methods: {
    init () {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm (id, title) {
      this.init()
      this.title = title
      if (id) {
        this.loading = true
        this.$http.post('/Base_BasicData/Base_UnitGroup/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
        })
      }
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/Base_BasicData/Base_UnitGroup/SaveData', this.entity).then(resJson => {
          this.loading = false
          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.loading = false
            this.visible = false
	        this.$emit('returnMain')
          } else {
            this.$message.error(resJson.Msg)
            this.loading = false
          }
        })
      })
    },
    // 非空校验
    validEmpty (index, name, tips) {
      return (rule, value, callback) => {
        const data = this.DetaiData[index][name]
        if (!data) {
          this.$message.warning('列表第' + (index + 1) + '行：' + tips)
          callback(tips || '必填')
        } else {
          callback()
        }
      }
    }
  }
}
</script>
<style scoped>
</style>
